package com.imgcrop;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.edmodo.cropper.CropImageView;

public class CropImgActivity extends AppCompatActivity {
    public CropImageView crop_cropImage;
    private String filePath;//需要剪裁的图片路径

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_img);

        Intent intent = getIntent();
        filePath = intent.getStringExtra("path");
        initView();
    }

    private void initView() {
        crop_cropImage = findViewById(R.id.crop_cropImage);
        crop_cropImage.setAspectRatio(1, 1);
        crop_cropImage.setFixedAspectRatio(true);
        findViewById(R.id.crop_sure).setOnClickListener(mClickListener);
        findViewById(R.id.crop_cancel).setOnClickListener(mClickListener);
        Bitmap bitmap = ImageToolUtils.getBitmapFromFile(filePath);
        crop_cropImage.setImageBitmap(bitmap);
    }

    private final PerfectClickListener mClickListener = new PerfectClickListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        protected void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.crop_sure:
                    //确定剪裁
                    Bitmap bit = crop_cropImage.getCroppedImage();
                    //压缩图片
                    ImageToolUtils.compressBitmap(bit, ImageToolUtils.getCropdFilePath(CropImgActivity.this), 500);
                    setResult(RESULT_OK);
                    finish();
                    break;
                case R.id.crop_cancel:
                    finish();
                    break;
            }
        }
    };
}
